package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.OrderItem;
import com.ruoyi.system.domain.Seat;
import com.ruoyi.system.service.IOrderItemService;
import com.ruoyi.system.service.ISeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单Controller
 *
 * @author ...
 * @date 2022-03-17
 */
@RestController
@RequestMapping("/system/item")
public class OrderItemController extends BaseController {
    @Autowired
    private IOrderItemService orderItemService;
    @Autowired
    private ISeatService seatService;

    /**
     * 查询订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(OrderItem orderItem) {
        startPage();
        List<OrderItem> list = orderItemService.selectOrderItemList(orderItem);
        return getDataTable(list);
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(orderItemService.selectOrderItemById(id));
    }

    /**
     * 我的订单
     * @return
     */
    @GetMapping("/myOrder")
    public AjaxResult getMyOrder(){
        List<OrderItem> myOrder = orderItemService.getMyOrder();
        return AjaxResult.success(myOrder);
    }

    /**
     * 新增订单
     */
    @PostMapping
    public AjaxResult add(@RequestBody OrderItem orderItem) {
        Seat seat = seatService.selectSeatById(orderItem.getSeatId());
        orderItem.setSeatName("第"+seat.getRow()+"排第"+seat.getColumn()+"列");
        orderItem.setCreateUser(SecurityUtils.getUserId().intValue());
        orderItem.setCreateTime(DateUtils.getNowDate());
        return toAjax(orderItemService.insertOrderItem(orderItem));
    }

    /**
     * 修改订单
     */
    @PutMapping
    public AjaxResult edit(@RequestBody OrderItem orderItem) {
        if (orderItem.getStatus().equals("拒绝")){
            Seat seat = seatService.selectSeatById(orderItem.getSeatId());
            seat.setStatus("空闲");
            seatService.updateSeat(seat);
        }
        orderItem.setUpdateUser(SecurityUtils.getUserId().intValue());
        orderItem.setUpdateTime(DateUtils.getNowDate());
        return toAjax(orderItemService.updateOrderItem(orderItem));
    }

    /**
     * 取消预定
     * @param orderItem
     * @return
     */
    @PostMapping("/cancel")
    public AjaxResult cancel(@RequestBody OrderItem orderItem) {
        Seat seat = seatService.selectSeatById(orderItem.getSeatId());
        seat.setStatus("空闲");
        seatService.updateSeat(seat);
        orderItem.setUpdateUser(SecurityUtils.getUserId().intValue());
        orderItem.setUpdateTime(DateUtils.getNowDate());
        orderItem.setStatus("已取消");
        orderItem.setIsDeleted(1);
        return toAjax(orderItemService.updateOrderItem(orderItem));
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(orderItemService.deleteOrderItemByIds(ids));
    }
}
